package sdk.webview.fmc.com.fmcsdk.base;

import android.content.SharedPreferences;

import rx.Observable;
import sdk.webview.fmc.com.fmcsdk.util.Constant;
import sdk.webview.fmc.com.fmcsdk.util.FMCLog;
import sdk.webview.fmc.com.fmcsdk.util.retrofit2.observer.ApiFactory;

/**
 * Created by ydm on 2017/7/31.
 * Mvp的presenter基类
 */

public abstract class BasePresenter <T extends BaseView>{
    public FMCLog logger;
    protected T view;
    private SharedPreferences sharedPrefs;

    /**
     * 将Presenter与MVP的View绑定起来.
     *
     * @param view MVPView
     */
    public void attachView(T view,FMCLog logger) {
        this.view = view;
        this.logger = logger;
    }

    /**
     * 将Presenter与MVP的View解除.
     */
    public void detachView() {
        view = null;
    }


    public T getView() {
        return view;
    }



    /**
     * 当View与Presenter绑定时回调, 此时界面控件等均未初始化
     */
    protected void onViewAttach() {}

    protected void onViewStart() {}

    protected void onViewResume() {}

    protected void onViewPause() {}

    protected void onViewStop() {}

    protected void onViewDetach() {}

    protected <V> Observable.Transformer<V, V> bindToLifeCycle() {
        return view.bind();
    }

    protected <V> Observable.Transformer<V, V> bindToLifeCycle(int type) {
        return null;
    }

    /**
     * 获取Retrofit Api接口
     */
    protected <T> T getApi(Class<T> clz) {
        return ApiFactory.getFactory().create(clz);
    }

    /**
     * 获取默认服务器地址
     * @return
     */
    public  String  getDefaultAddress(){
//        return "http://192.168.200.35";
        return  sharedPrefs.getString(Constant.BASE_ADDRESS_URL,"");
    }

    /**
     * 获取首选项
     * @return
     */
    public  SharedPreferences  getSharedPrefs(){
        return sharedPrefs;
    }

    /**
     * 设置首选项
     * @return
     */
    public void setSharedPrefs(SharedPreferences sharedPrefs) {
        this.sharedPrefs = sharedPrefs;
    }



}
